/*
 * ikakeibo 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.model;

import java.util.Vector;

import com.wateray.ipassbook.commom.Constant;
import com.wateray.ipassbook.domain.form.ReceiptForm;

/**
 * @author wateray
 * @create 2009-5-2
 */
public class ReceiptTabelModel extends AbstractPassbookTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3595146945594549164L;

	public ReceiptTabelModel(Vector<String> columnNames){
		super(columnNames);

	}
	@SuppressWarnings("unchecked")
	public ReceiptTabelModel(Vector dataVector, Vector<String> columnNames){
		super(dataVector, columnNames);
	}


	public boolean isCellEditable(int rowIndex, int columnIndex) {
		// Note that the data/cell address is constant,
		// no matter where the cell appears onscreen.
		ReceiptForm entity = (ReceiptForm) (dataVector.get(rowIndex));
		String kbn = entity.getReceiptFlag();
		switch (columnIndex) {
		case 0:
			return false;
		case 3:
			return kbn.equals(Constant.INCOME_KBN);
		case 4:
			return kbn.equals(Constant.EXPENSE_KBN);
		default:
			return true;
		}
	}

	

	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		// data[rowIndex][columnIndex] = value;
		
		//ADD 2009/05/09
		if(value == null || value.equals(this.getValueAt(rowIndex, columnIndex))){
			return;
		}

		ReceiptForm entity = (ReceiptForm) (dataVector.get(rowIndex));
		switch (columnIndex) {
		case 1:
			entity.setReceiptDay(value.toString());
			break;
		case 2:
			entity.setSubjectName(value.toString());
			break;
		case 3:
			entity.setAmount(Double.parseDouble(value.toString()));
			break;
		case 4:
			entity.setAmount(Double.parseDouble(value.toString()));
			break;
		case 5:
			entity.setCurrencyName(value.toString());
			break;
		case 6:
			entity.setPassbookName(value.toString());
			break;
		case 7:
			entity.setMemo(value.toString());
			break;
		}
		
		 fireTableCellUpdated(rowIndex, columnIndex);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		// return data[rowIndex][columnIndex];
		Object value = null;

		//UPD 2009/05/28 rowIndex
		//System.out.println(this.getClass().getSimpleName() + " [rowIndex=" + rowIndex + ", columnIndex=" +columnIndex +"]");
		ReceiptForm entity = (ReceiptForm) (dataVector.get(rowIndex));
		String kbn = entity.getReceiptFlag();

		switch (columnIndex) {
		case 0:
			value = rowIndex + 1;
			break;
		case 1:
			value = entity.getReceiptDay();
			break;
		case 2:
			value = entity.getSubjectName();
			break;
		case 3:
			value = kbn.equals(Constant.INCOME_KBN) ? entity.getAmount()
					: 0.0;
			break;
		case 4:
			value = kbn.equals(Constant.EXPENSE_KBN) ? entity.getAmount()
					: 0.0;
			break;
		case 5:
			value = entity.getCurrencyName();
			break;
		case 6:
			value = entity.getPassbookName();
			break;
		case 7:
			value = entity.getMemo();
			break;
		}
		return value;
	}
}
